package com.example.springbott.service;

import com.example.springbott.bean.MarketSystem;
import com.example.springbott.bean.MarketSystemExample;
import com.example.springbott.bean.common.*;
import com.example.springbott.mapper.*;
import com.example.springbott.util.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author 1784549954@qq.com
 * @since 2022/11/20 16:28
 */
@Service
public class AdminConfigServiceImpl implements AdminConfigService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketSystemMapper marketSystemMapper;

    //------------------------------heheer--------------------------------------


    @Override
    public ConfigMallVO configMall() {
        ConfigMallVO configMallVO = new ConfigMallVO();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameIn(Arrays.asList(Count.MALL_SYSTEM_ADDRESS, Count.MALL_SYSTEM_LATITUDE,
                Count.MALL_SYSTEM_NAME, Count.MALL_SYSTEM_QQ,
                Count.MALL_SYSTEM_PHONE, Count.MALL_SYSTEM_LONGITUDE));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            String keyName = marketSystem.getKeyName();
            if (keyName.equals(Count.MALL_SYSTEM_ADDRESS)) {
                configMallVO.setMarket_mall_address(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_LATITUDE)) {
                configMallVO.setMarket_mall_latitude(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_NAME)) {
                configMallVO.setMarket_mall_name(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_LONGITUDE)) {
                configMallVO.setMarket_mall_longitude(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_QQ)) {
                configMallVO.setMarket_mall_qq(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_PHONE)) {
                configMallVO.setMarket_mall_phone(marketSystem.getKeyValue());
            }
        }
        return configMallVO;
    }

    @Transactional
    @Override
    public void configMallPost(ConfigMallVO configMall) throws IllegalAccessException {
        Map map = addMap(configMall);
        Set<String> strings = map.keySet();
        updateByExampleSelective(map, strings);
    }


    @Override
    public ConfigExpressVO configExpress() {

        ConfigExpressVO configExpressVO = new ConfigExpressVO();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameIn(Arrays.asList(Count.MALL_SYSTEM_EXPRESS_FREIGHT_MIN, Count.MALL_SYSTEM_EXPRESS_FREIGHT_VALUE));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            String keyName = marketSystem.getKeyName();
            if (keyName.equals(Count.MALL_SYSTEM_EXPRESS_FREIGHT_VALUE)) {
                configExpressVO.setMarket_express_freight_value(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_EXPRESS_FREIGHT_MIN)) {
                configExpressVO.setMarket_express_freight_min(marketSystem.getKeyValue());
            }
        }
        return configExpressVO;
    }

    @Override
    public void configExpressPost(ConfigExpressVO configExpressVO) throws IllegalAccessException {
        Map map = addMap(configExpressVO);
        Set<String> strings = map.keySet();
        updateByExampleSelective(map, strings);

    }

    @Override
    public ConfigOrderVO configOrder() {
        ConfigOrderVO configOrderVO = new ConfigOrderVO();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameIn(Arrays.asList(Count.MALL_SYSTEM_ORDER_COMMENT, Count.MALL_SYSTEM_ORDER_UNCONFIRM, Count.MALL_SYSTEM_ORDER_UNPAID));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            String keyName = marketSystem.getKeyName();
            if (keyName.equals(Count.MALL_SYSTEM_ORDER_COMMENT)) {
                configOrderVO.setMarket_order_comment(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_ORDER_UNCONFIRM)) {
                configOrderVO.setMarket_order_unconfirm(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_ORDER_UNPAID)) {
                configOrderVO.setMarket_order_unpaid(marketSystem.getKeyValue());
            }

        }
        return configOrderVO;
    }

    @Override
    @Transactional
    public void configOrderPost(ConfigOrderVO configOrderVO) throws IllegalAccessException {
        Map map = addMap(configOrderVO);

        Set<String> strings = map.keySet();
        updateByExampleSelective(map, strings);


    }

    @Override
    public ConfigWxVO configWx() {
        ConfigWxVO configWxVO = new ConfigWxVO();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameIn(Arrays.asList(Count.MALL_SYSTEM_WX_INDEX_NEW, Count.MALL_SYSTEM_WX_INDEX_BRAND,
                Count.MALL_SYSTEM_WX_CATLOG_GOODS, Count.MALL_SYSTEM_WX_CATLOG_LIST,
                Count.MALL_SYSTEM_WX_INDEX_HOT, Count.MALL_SYSTEM_WX_INDEX_TOPIC,
                Count.MALL_SYSTEM_WX_SHARE));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        for (MarketSystem marketSystem : marketSystems) {
            String keyName = marketSystem.getKeyName();
            if (keyName.equals(Count.MALL_SYSTEM_WX_SHARE)) {
                configWxVO.setMarket_wx_share(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_WX_INDEX_TOPIC)) {
                configWxVO.setMarket_wx_index_topic(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_WX_INDEX_HOT)) {
                configWxVO.setMarket_wx_index_hot(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_WX_CATLOG_LIST)) {
                configWxVO.setMarket_wx_catlog_list(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_WX_CATLOG_GOODS)) {
                configWxVO.setMarket_wx_catlog_goods(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_WX_INDEX_BRAND)) {
                configWxVO.setMarket_wx_index_brand(marketSystem.getKeyValue());
            } else if (keyName.equals(Count.MALL_SYSTEM_WX_INDEX_NEW)) {
                configWxVO.setMarket_wx_index_new(marketSystem.getKeyValue());
            }
        }
        return configWxVO;
    }


    @Override
    @Transactional
    public void configWxPost(ConfigWxVO configWxVO) throws IllegalAccessException {
        Map map = addMap(configWxVO);
        Set<String> strings = map.keySet();
        updateByExampleSelective(map, strings);
    }

    private void updateByExampleSelective(Map<String, String> map, Set<String> strings) {
        for (String string : strings) {
            MarketSystem marketSystem = new MarketSystem();
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            marketSystem.setKeyValue(map.get(string));
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            criteria.andKeyNameEqualTo(string);
            marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        }
    }


    public Map addMap(Object object) throws IllegalAccessException {
        HashMap<String, String> map = new HashMap<>();
        Class<?> aClass = object.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            String value = (String) declaredField.get(object);
            map.put(name, value);
        }
        return map;
    }

    @Override
    public DashboardVO dashboard() {
        DashboardVO dashboardVO = new DashboardVO();
        dashboardVO.setGoodsTotal(marketGoodsMapper.countByExample(null));
        dashboardVO.setUserTotal(marketUserMapper.countByExample(null));
        dashboardVO.setProductTotal(marketGoodsProductMapper.countByExample(null));
        dashboardVO.setOrderTotal(marketOrderMapper.countByExample(null));
        return dashboardVO;
    }
}
